﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data;
using MaxLeaf.DB;

namespace MaxLeaf.DAL
{
    public class ImInvMasterDALBuilder : BaseDALBuilder
    {
        
        public ImInvMasterDALBuilder():base()
        {
            K3DAL = new K3.ImInvMasterDAL();

            WebDAL = new Web.ImInvMasterDAL();

        }

        protected override bool ValidCheck(ServerInfo orgServer, ServerInfo destServer, DataRowView drvLog)
        {
            if (CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == (int)LogStatus.Delete)
            {
                return true;
            }
            if (destServer.Type == ServerType.Web)
            {
                using (DBAccess dbOrgCon = new DBAccess(orgServer))
                {
                    DataRow drT_Item = dbOrgCon.GetTableDataFirstRow("T_Item", new Dictionary<string, object>() { { "FItemID", drvLog[DEL.LogDEL.KeyValue] } }, "Fnumber,FDetail");
                    if (drT_Item == null)
                    {
                        LogManager.Debug(string.Format("FItemID {0} 不是T_Item", drvLog[DEL.LogDEL.KeyValue]));
                        return false;
                    }
                    if (!CvtFunc.ToBoolean(drT_Item["FDetail"]))
                    {

                        LogManager.Debug(string.Format("Fnumber {0} 不是物料，不分发", drT_Item["Fnumber"]));
                        return false;

                    }

                    DataRow drT_icItem = dbOrgCon.GetTableDataFirstRow("T_icItem", new Dictionary<string, object>() { { "FItemID", drvLog[DEL.LogDEL.KeyValue] } }, "Fnumber,F_136,f_125,f_138");
                    if (drT_icItem == null)
                    {
                        LogManager.Debug(string.Format("FItemID {0} 不是物料", drvLog[DEL.LogDEL.KeyValue]));
                        return false;
                    }
                    string strItemParentFnumber = GetItemParentFnumber(drT_icItem);
                    if (!strItemParentFnumber.StartsWith("9."))
                    {
                        //20111010接口需求_1, 北京.2 武汉.4, 取消F_136（网站目录分类）字段的传递到网站。
                        LogManager.Debug(string.Format("Fnumber {0} 不分发", drT_Item["Fnumber"]));
                        return false;

                        //if (CvtFunc.ToString(drT_icItem["f_136"]).Length == 0)
                        //{
                        //    //sku
                        //    if (CvtFunc.ToString(drT_icItem["f_125"]).Length == 0)
                        //    {
                        //        LogManager.Debug(string.Format("Fnumber {0} sku is empty", drT_icItem["Fnumber"]));
                        //        return false;
                        //    }
                        //    LogManager.Debug(string.Format("Fnumber {0} 网站目录分类 is empty", drT_icItem["Fnumber"]));
                        //    return false;
                        //}
                        //else
                        //{
                        //    //sku
                        //    if (CvtFunc.ToString(drT_icItem["f_125"]).Length == 0)
                        //    {
                        //        LogManager.Debug(string.Format("Fnumber {0} sku is empty", drT_icItem["Fnumber"]));
                        //        return false;
                        //    }
                        //}
                    }


                }
            }
            else if (destServer.Type == ServerType.K3)
            {
                using (DBAccess dbOrgCon = new DBAccess(orgServer))
                {

                    if (!CheckItem(dbOrgCon,CvtFunc.ToString(drvLog[DEL.LogDEL.KeyValue])))
                    {
                        return false;
                    }
                    


                }
            }

            return base.ValidCheck(orgServer,destServer, drvLog);
        }

        public  bool CheckItem(DBAccess dbOrgCon,string strFItemId)
        {

            DataRow drT_Item = dbOrgCon.GetTableDataFirstRow("T_Item", new Dictionary<string, object>() { { "FItemID", strFItemId } }, "Fnumber,FDetail");
            if (drT_Item == null)
            {
                LogManager.Debug(string.Format("FItemID {0} 不是T_Item", strFItemId));
                return false;
            }
            if (!CvtFunc.ToBoolean(drT_Item["FDetail"]))
            {

                LogManager.Debug(string.Format("Fnumber {0} 不是物料，不分发", drT_Item["Fnumber"]));
                return false;

            }

            DataRow drT_icItem = dbOrgCon.GetTableDataFirstRow("T_icItem", new Dictionary<string, object>() { { "FItemID", strFItemId } }, "Fnumber,F_136,f_125,f_138");
            if (drT_icItem == null)
            {
                LogManager.Debug(string.Format("FItemID {0} 不是物料", strFItemId));
                return false;
            }

            if (!CvtFunc.ToString(drT_icItem["Fnumber"]).StartsWith("3.") && !CvtFunc.ToString(drT_icItem["Fnumber"]).StartsWith("5.") && !CvtFunc.ToString(drT_icItem["Fnumber"]).StartsWith("9."))
            {

                LogManager.Debug(string.Format("Fnumber {0} 不分发", drT_icItem["Fnumber"]));
                return false;

            }

            return true;
        }
        protected override bool ValidCheckOrgData(ServerInfo orgServer, DataRowView drvLog)
        {
            if (!orgServer.MasterDB)
            {
                //主账套分发
                LogManager.Debug(string.Format("{0} MasterDB {1}   ", orgServer,orgServer.MasterDB));
                return false;
            }
            if (CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == (int)LogStatus.Delete)
            {
                return true;
            }
             
            return base.ValidCheckOrgData(orgServer, drvLog);
        }

        private string GetItemParentFnumber(DataRow drT_icItem)
        {
            string strParentFnumber = CvtFunc.ToString(drT_icItem["Fnumber"]);
            
                int iEd = strParentFnumber.LastIndexOf('.');

                strParentFnumber = CvtFunc.ToLeftString(strParentFnumber, iEd);
            
            return strParentFnumber;
        }

        
    }
}